package util

import (
	"encoding/json"
	"fmt"
	"net"
	"strconv"
	"strings"
	"time"
)

//RandomStr 获取一个随机字符串
func RandomStr() string {
	return fmt.Sprintf("%d", time.Now().UnixNano())
}

// LocalIP 获取机器的IP
func LocalIP() string {
	info, _ := net.InterfaceAddrs()
	for _, addr := range info {
		ipNet, ok := addr.(*net.IPNet)
		if !ok {
			continue
		}
		if !ipNet.IP.IsLoopback() && ipNet.IP.To4() != nil {
			return ipNet.IP.String()
		}
	}
	return ""
}

func MapStringToStruct(m map[string]string, i interface{}) error {
	bin, err := json.Marshal(m)
	if err != nil {
		return err
	}
	err = json.Unmarshal(bin, i)
	if err != nil {
		return err
	}
	return nil
}

func TruncatedText(data string, length int) string {
	data = FilterTheSpecialSymbol(data)
	if len([]rune(data)) > length {
		return string([]rune(data)[:length-1])
	}
	return data
}

//过滤特殊符号
func FilterTheSpecialSymbol(data string) string {
	// 定义转换规则
	specialSymbol := func(r rune) rune {
		if r == '`' || r == '[' || r == '~' || r == '!' || r == '@' || r == '#' || r == '$' ||
			r == '^' || r == '&' || r == '*' || r == '~' || r == '(' || r == ')' || r == '=' ||
			r == '~' || r == '|' || r == '{' || r == '}' || r == '~' || r == ':' || r == ';' ||
			r == '\'' || r == ',' || r == '\\' || r == '[' || r == ']' || r == '.' || r == '<' ||
			r == '>' || r == '/' || r == '?' || r == '~' || r == '！' || r == '@' || r == '#' ||
			r == '￥' || r == '…' || r == '&' || r == '*' || r == '（' || r == '）' || r == '—' ||
			r == '|' || r == '{' || r == '}' || r == '【' || r == '】' || r == '‘' || r == '；' ||
			r == '：' || r == '”' || r == '“' || r == '\'' || r == '"' || r == '。' || r == '，' ||
			r == '、' || r == '？' || r == '%' || r == '+' || r == '_' || r == ']' || r == '"' || r == '&' {
			return ' '
		}
		return r
	}
	data = strings.Map(specialSymbol, data)
	return strings.Replace(data, "\n", " ", -1)
}

// 支付宝金额(分)转字符串(元)
func MoneyFeeToYuanString(moneyFee int64) string {
	if moneyFee <= 0 {
		return "0"
	}
	s := strconv.FormatInt(moneyFee, 10)

	if len(s) == 1 {
		s = fmt.Sprintf("0.0%s", s)
	} else if len(s) == 2 {
		s = fmt.Sprintf("0.%s", s)
	} else {
		s = fmt.Sprintf("%s.%s", s[0:len(s)-2], s[len(s)-2:])
	}

	for {
		switch s[len(s)-1] {
		case '0':
			s = s[0 : len(s)-1]
		case '.':
			s = s[0 : len(s)-1]
			return s
		default:
			return s
		}
	}

	return s
}

// 支付宝金额(分)转字符串(分)
func MoneyFeeToFenString(moneyFee int64) string {
	if moneyFee <= 0 {
		return "0"
	}
	s := strconv.FormatInt(moneyFee, 10)

	return s
}

// 字符串转元金额分
func ParseMoneyYuan(data string) int64 {
	var (
		i, j int64
		ss   []string
		err  error
	)
	if data == "" {
		return 0
	}
	ss = strings.Split(data, ".")
	if len(ss) == 0 || len(ss) > 2 {
		return 0
	}

	i, err = strconv.ParseInt(ss[0], 10, 0)
	if err != nil || i < 0 {
		return 0
	}
	if len(ss) == 2 {
		if len(ss[1]) == 0 || len(ss[1]) > 2 {
			return 0
		}
		j, err = strconv.ParseInt(ss[1], 10, 0)
		if err != nil || j < 0 {
			return 0
		}
		if len(ss[1]) == 1 {
			j = j * 10
		}
	}
	return i*100 + j
}

// 字符串分转金额分
func ParseMoneyFen(data string) int64 {
	var (
		result int64
	)
	result, _ = strconv.ParseInt(data, 10, 0)
	return result
}
